package com.hfzy.ihk.web.freeswitchManage.conf;

import com.hfzy.ihk.common.support.redis.RedisConfig;
import com.hfzy.ihk.common.web.mvc.CommonMvcConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

import java.net.URL;

/**
 * Created by Administrator on 2018/1/16.
 *
 * @author wws
 */
@Configuration
@Import(RedisConfig.class)
public class MvcConfig extends CommonMvcConfigurer implements WebMvcConfigurer
{

    @Autowired
    ApplicationContext applicationContext;

 /*   @Bean
    @Profile({"local"})
    public ViewResolver viewResolver(SpringTemplateEngine templateEngine)
    {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine);
        viewResolver.setCache(false);
        return viewResolver;
    }
    @Bean
    @Profile({"local"})
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }*/

    @Bean
    @Profile({"local"})
    public ITemplateResolver templateResolver()
    {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(this.applicationContext);
        URL resource = getClass().getClassLoader().getResource("templates/");
        String devResource = resource.getFile().toString().replaceAll("build/resources/main", "src/main/resources");
        resolver.setPrefix("file:" + devResource);
        resolver.setCacheable(false);
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML");
        return resolver;
    }
    @Bean
    @Profile({"local"})
    public ITemplateResolver templateResolver1() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(this.applicationContext);
        URL resource = getClass().getClassLoader().getResource("static/");
        String devResource = resource.getFile().toString().replaceAll("build/resources/main", "src/main/resources");
        resolver.setPrefix("file:" + devResource);
        resolver.setCacheable(false);
        resolver.setSuffix(".js");
        resolver.setTemplateMode("HTML");
        return resolver;
    }
}